"""
encoding = 'utf-8'
@author : ffine
@time   : 2022-04-30  20:16
@IDEA   : PyCharm
@goal   : learning datetime module
"""
import datetime
import time

print("{:=^50s}".format("datetime.date"))
d = datetime.date.today()
print(d,type(d),)  # 2022-04-30

d = datetime.date(2019, 4, 30)
print(d,type(d),)  # 2019-04-30

d = datetime.date.fromtimestamp(time.time())
print(d,type(d),)  # 2022-04-30

# 类属性
print(datetime.date.min)  # 0001-01-01
print(datetime.date.max)  # 99-12-31
print(datetime.date.resolution)  # 1 day  最小时间差

# 实例属性
print(d.year)  # 2022
print(d.month)  # 4
print(d.day)  # 30
print(d.weekday())  # 4
print(d.isoweekday())  # 5
print(d.isocalendar())  # (2022, 5, 1)
print(d.isoformat())  # 2022-04-30
print(d.ctime())  # Sun Apr 30 22:00:00 2022
print(d.strftime('%Y-%m-%d'))  # 2022-04-30
print(d.strftime('%Y-%m-%d %H:%M:%S'))  # 2022-04-30 22:00:00

# 实例方法
# datetime.date对象 > 结构化对象
print(d.timetuple())

# 其他方法
print(d.replace(year=2020))  # 2020-04-30
print(d.replace(month=5))  # 2022-05-30
print(d.replace(day=1))  # 2022-04-01
print(d.toordinal())  # 从 0001-01-01 开始的天数
print(d.fromordinal(3))  # 从 0001-01-03
print(d.weekday())  # 5:周六
print(d.isoweekday())  # 6:周六
print(d.isocalendar())  # (2022, 17, 6)(year, week number, and weekday).
print(d.isoformat())  # 2022-04-30


print("{:=^50s}".format("datetime.time"))
t = datetime.time(22, 30, 30)
print(t,type(t),)  # 22:30:30

# 类方法
print(datetime.time.min)  # 00:00:00
print(datetime.time.max)  # 23:59:59
print(datetime.time.resolution)  # 1 microsecond

# 实例属性
print(t.hour)  # 22
print(t.minute)  # 30
print(t.second)  # 30
print(t.microsecond)  # 0

# 其他方法
print(t.replace(hour=23))  # 23:30:30
print(t.replace(minute=59))  # 22:59:30
print(t.replace(second=59))  # 22:30:59
print(t.replace(microsecond=1))  # 22:30:30.000001
print(t.isoformat())  # 22:30:30
print(t.strftime('%H:%M:%S'))  # 22:30:30

print("{:=^50s}".format("datetime.datetime"))
dt = datetime.datetime(2022, 4, 30, 20, 42, 30)
print(dt,type(dt))  # 2022-04-30 22:30:30
dt = datetime.datetime.now()
print(dt,type(dt))  # 2022-04-30 20:44:23.083988 <class 'datetime.datetime'>
dt = datetime.datetime.today()
print(dt,type(dt))  # 2022-04-30 20:44:23.083994 <class 'datetime.datetime'>
dt = datetime.datetime.now(tz=datetime.timezone.utc)
print(dt,type(dt))  # 2022-04-30 12:44:23.083994 <class 'datetime.datetime'>
dt = datetime.datetime.combine(d,t)  # 将日期和时间合并
print(dt,type(dt))  # 2022-04-30 22:30:30 <class 'datetime.datetime'>

# 实例属性
print(dt.year)  # 2022
print(dt.month)  # 4
print(dt.day)  # 30
print(dt.hour)  # 22
print(dt.minute)  # 30
print(dt.second)  # 30
print(dt.microsecond)  # 0
print(dt.tzinfo)  # None

# 实例方法
print(dt.replace(year=2020))  # 2020-04-30 22:30:30

# dt > 结构化对象s
print(dt.timetuple())  # time.struct_time(tm_year=2022, tm_mon=4, tm_mday=30, tm_hour=22, tm_min=30, tm_sec=30, tm_wday=4, tm_yday=99, tm_isdst=-1)
# dt > 时间戳
print(dt.timestamp())  # 1651329030.0
# dt > 格式化字符串
print(dt.strftime('%Y-%m-%d %H:%M:%S'))  # 2022-04-30 22:30:30

# 时间戳 > datetime
print(datetime.datetime.fromtimestamp(time.time()))  # 2022-04-30 20:53:15.001548 <class 'datetime.datetime'>
# 格式化对象 > datetime
print(datetime.datetime.strptime('2022-04-30 22:30:30', '%Y-%m-%d %H:%M:%S'))  # 2022-04-30 22:30:30
# 结构化对象 > datetime
print(datetime.datetime.fromtimestamp(time.mktime(time.localtime())))  # 2022-04-30 20:53:15.001548 <class 'datetime.datetime'>

print("{:=^50s}".format("datetime.timedelta"))
# 生成时间差 td = datetime.timedelta(days=1, hours=2, minutes=3, seconds=4, microseconds=5)
print(datetime.timedelta(days=1, hours=2, minutes=3, seconds=4, microseconds=5))  # 1 days, 2:03:04.000005
td = datetime.timedelta(days=-5,hours=-2)
print(td)  # -6 days, 22:00:00
td = datetime.timedelta(weeks=3)
print(td)  # 21 days, 0:00:00


# 计算目标日期
dt  = datetime.datetime.now()
print(dt)  # 2022-04-30 20:44:23.083994
print(dt + datetime.timedelta(days=10))  # 2022-04-31 20:44:23.083994

# 计算时间差
dt1 = datetime.datetime.now()
dt2 = datetime.datetime.utcnow()
print(dt1 - dt2)  # 7:59:59.999998
print((dt1 - dt2).seconds/3600)  # 几小时:7.999722222222222